<?php
/**
 * 字符串
 * @App
 * @Group
 * @Action SetTrait
 *
 */

namespace app\common\redis\traits;

trait StrTrait
{
    /**
     * Redis 操作句柄
     * @var \Redis
     */
    public $redis_handle;

    /**
     * 设置字符串类类型 @todo 和驱动的set不一致
     * @param string|number $item
     * @param string $key
     * @return bool
     */
    public function Set($item, $key = '', $outTime = 0)
    {
        $_key = $key ?: $this->key;
        if ($outTime <= 0)
            return $this->redis_handle->set($_key, $item);

        return $this->redis_handle->setex($_key, $outTime, $item);
    }

    /**
     * 判断写入值是否重复
     * @param $item
     * @param string $key
     * @return bool
     */
    public function Setnx( $item, $key = '' )
    {
        $_key = $key ?: $this->key;
        return $this->redis_handle->setnx( $_key, $item );
    }

    /**
     * 设置一个Key的活跃时间
     * @param $item
     * @param string $key
     * @return bool
     */
    public function Expire( $item, $key = '' )
    {
        $_key = $key ?: $this->key;
        return $this->redis_handle->expire( $_key, $item );
    }

    /**
     * 获取字符串类型
     * @param string $key
     * @return bool|string
     */
    public function Get( $key='' ) {
        $_key = $key ?: $this->key;
        return $this->redis_handle->get( $_key );
    }

    /**
     * 字符串值+1
     * @param string $key
     * @return int
     */
    public function Incr( $key = "" ){
        $_key = $key ?: $this->key;
        return $this->redis_handle->INCR( $_key );
    }
}